package cn.alibab.cangku.service.impl;


import cn.alibab.cangku.dao.CreateId;
import cn.alibab.cangku.dao.ProductDao;
import cn.alibab.cangku.dao.SupplierDao;
import cn.alibab.cangku.mapper.BizInStockMapper;
import cn.alibab.cangku.mapper.BizProductMapper;
import cn.alibab.cangku.mapper.BizProductStockMapper;
import cn.alibab.cangku.mapper.BizSupplierMapper;
import cn.alibab.cangku.model.*;
import cn.alibab.cangku.service.ProductService;
import cn.alibab.cangku.vo.ProductInputVo;
import cn.alibab.cangku.vo.ProductVo;
import cn.alibab.common.model.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author Xd
 * @create 2020-07-10 11:33
 */
@Service
public class ProductServiceImpl implements ProductService {
    private final ProductDao productDao;
    private final BizInStockMapper bizInStockMapper;
    private final BizSupplierMapper bizSupplierMapper;
    private final SupplierDao supplierDao;
    private final BizProductMapper bizProductMapper;
    private final BizProductStockMapper bizProductStockMapper;
    private final CreateId createId;

    public ProductServiceImpl(ProductDao productDao, BizInStockMapper bizInStockMapper, BizSupplierMapper bizSupplierMapper, SupplierDao supplierDao, BizProductMapper bizProductMapper, BizProductStockMapper bizProductStockMapper, CreateId createId) {
        this.productDao = productDao;
        this.bizInStockMapper = bizInStockMapper;
        this.bizSupplierMapper = bizSupplierMapper;
        this.supplierDao = supplierDao;
        this.bizProductMapper = bizProductMapper;
        this.bizProductStockMapper = bizProductStockMapper;
        this.createId = createId;
    }
//查询所有物品库存
    @Override
    public R getAllProduct() {
        List<ProductVo> productVoList =  productDao.getAllProduct();
        return R.ok(productVoList);
    }
    //查询所有入库的物品
    @Override
    public R getAllProductInput() {
        BizInStockExample example = new BizInStockExample();
        BizInStockExample.Criteria criteria = example.createCriteria();
        List<BizInStock> bizInStockList = bizInStockMapper.selectByExample(example);
        System.out.println(bizInStockList.size()+"======");
        List<ProductInputVo> productInputVoList = new ArrayList<>();
        IntStream.range(0, bizInStockList.size()).forEach(i -> {
            ProductInputVo productInputVo = new ProductInputVo();
            BeanUtils.copyProperties(bizInStockList.get(i), productInputVo);
            System.out.println(bizInStockList.get(i).getSupplierId());
            BizSupplier bizSupplier = findAllSupplier(bizInStockList.get(i).getSupplierId());
            BeanUtils.copyProperties(bizSupplier, productInputVo);
            productInputVoList.add(productInputVo);
        });


        return R.ok(productInputVoList);
    }
    //请输入入库单查询
    @Override
    public R getProductByInNum(String inNum) {
        BizInStockExample example = new BizInStockExample();
        BizInStockExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(inNum)){
            criteria.andInNumLike("%"+inNum +"%");
        }
        List<BizInStock> bizInStockList = bizInStockMapper.selectByExample(example);
        List<ProductInputVo> productInputVoList = new ArrayList<>();
        List<BizSupplier>  bizSupplierList = new ArrayList<>();
        for (int i = 0;i<bizInStockList.size();i++){
            ProductInputVo productInputVo = new ProductInputVo();
            BeanUtils.copyProperties(bizInStockList.get(i),productInputVo);
            BizSupplier bizSupplier = findAllSupplier(bizInStockList.get(i).getSupplierId());
            BeanUtils.copyProperties(bizSupplier,productInputVo);
            productInputVoList.add(productInputVo);
        }
        return R.ok(productInputVoList);
    }
    //物资入库
    @Transactional
    @Override
    public R putProductInPut(ProductInputVo productInputVo) {
//        System.out.println(productInputVo.getName()+"====1234");
        //通过前台获取捐赠的物品是什么
        String pName = productInputVo.getpName();
//        System.out.println(pName+"====123");
        BizProductStock productStockByPNume = findProductStockByPName(pName);
//        System.out.println(productStockByPNume.getStock()+"====111");
//        productStockByPNume.setStock(productStockByPNume.getStock()+ productInputVo.getProductNumber());
        Long productStock = productStockByPNume.getStock()+ productInputVo.getProductNumber();
         productStockByPNume.setStock(productStock);
        System.out.println(productInputVo.getProductNumber());
        System.out.println(productStockByPNume.getStock());
        System.out.println(productStock);
        bizProductStockMapper.updateByPrimaryKeySelective(productStockByPNume);

        BizSupplier bizSupplier= new BizSupplier();
        BizInStock bizInStock = new BizInStock();
        BeanUtils.copyProperties(productInputVo,bizInStock);
        BeanUtils.copyProperties(productInputVo,bizSupplier);
        bizSupplier.setCreateTime(new Date());
        bizSupplier.setModifiedTime(new Date());
        bizSupplier.setSort(1);
        bizSupplierMapper.insertSelective(bizSupplier);
        Long supplierId = bizSupplier.getId();
//        System.out.println(supplierId);
        bizInStock.setCreateTime(new Date());
        bizInStock.setInNum(createId.getId(1));
        bizInStock.setModified(new Date());
        bizInStock.setType(1);
        bizInStock.setRemark("245");
        bizInStock.setSupplierId(supplierId);
        bizInStockMapper.insertSelective(bizInStock);
        return R.ok();
    }
    //根据物资名称查询产品库存
    @Override
    public R getAllProductByName(String name) {
        BizProductExample example = new BizProductExample();
        BizProductExample.Criteria criteria = example.createCriteria();

        criteria.andPnameLike("%"+name +"%");
        List<BizProduct> bizProducts = bizProductMapper.selectByExample(example);
        List<ProductVo> productVoList = new ArrayList<>();
        for (int i = 0;i<bizProducts.size();i++){
            ProductVo productVo = new ProductVo();
            BizProductStock productStockByPNum = findProductStockByPNum(bizProducts.get(i).getpNum());
            BeanUtils.copyProperties(bizProducts.get(i),productVo);
            BeanUtils.copyProperties(productStockByPNum,productVo);
            productVoList.add(productVo);
        }
        return R.ok(productVoList);
    }

    //获取所有的捐献者信息
    public  BizSupplier findAllSupplier(Long id){
        System.out.println(id);
        return supplierDao.getSupplierById(id);
    }
    //根据pNum获取对应的库存信息
    public BizProductStock findProductStockByPNum(String pNum){
        return productDao.findProductStockByPNum(pNum);
    }
    //通过pNume获取对应的物资库存
    public BizProductStock findProductStockByPName(String pNume ){
        return productDao.findProductStockByPName(pNume);
    }

}
